<template>
	<view class="u-checkbox" :style="[checkboxStyle]">
		<view class="u-checkbox__icon-wrap" @tap="toggle" :class="[iconClass]" :style="[iconStyle]">
			<u-icon class="u-checkbox__icon-wrap__icon" name="checkbox-mark" :size="checkboxIconSize" :color="iconColor"/>
		</view>
		<view class="u-checkbox__label" @tap="onClickLabel" :style="{
			fontSize: $u.addUnit(labelSize)
		}">
			<slot />
		</view>
	</view>
</template>

<script>
	
	export default {
		name: "u-checkbox",
		props: {
			
			name: {
				type: [String, Number],
				default: ''
			},
			
			shape: {
				type: String,
				default: ''
			},
			
			value: {
				type: Boolean,
				default: false
			},
			
			disabled: {
				type: [String, Boolean],
				default: ''
			},
			
			labelDisabled: {
				type: [String, Boolean],
				default: ''
			},
			
			activeColor: {
				type: String,
				default: ''
			},
			
			iconSize: {
				type: [String, Number],
				default: ''
			},
			
			labelSize: {
				type: [String, Number],
				default: ''
			},
			
			size: {
				type: [String, Number],
				default: ''
			},
		},
		data() {
			return {
				parentDisabled: false,
				newParams: {},
			};
		},
		created() {
			
			this.parent = this.$u.$parent.call(this, 'u-checkbox-group');
			
			this.parent && this.parent.children.push(this);
		},
		computed: {
			
			isDisabled() {
				return this.disabled !== '' ? this.disabled : this.parent ? this.parent.disabled : false;
			},
			
			isLabelDisabled() {
				return this.labelDisabled !== '' ? this.labelDisabled : this.parent ? this.parent.labelDisabled : false;
			},
			
			checkboxSize() {
				return this.size ? this.size : (this.parent ? this.parent.size : 34);
			},
			
			checkboxIconSize() {
				return this.iconSize ? this.iconSize : (this.parent ? this.parent.iconSize : 20);
			},
			
			elActiveColor() {
				return this.activeColor ? this.activeColor : (this.parent ? this.parent.activeColor : 'primary');
			},
			
			elShape() {
				return this.shape ? this.shape : (this.parent ? this.parent.shape : 'square');
			},
			iconStyle() {
				let style = {};
				
				if (this.elActiveColor && this.value && !this.isDisabled) {
					style.borderColor = this.elActiveColor; 
					style.backgroundColor = this.elActiveColor;
				}
				style.width = this.$u.addUnit(this.checkboxSize);
				style.height = this.$u.addUnit(this.checkboxSize);
				return style;
			},
			
			iconColor() {
				return this.value ? '#ffffff' : 'transparent';
			},
			iconClass() {
				let classes = [];
				classes.push('u-checkbox__icon-wrap--' + this.elShape);
				if (this.value == true) classes.push('u-checkbox__icon-wrap--checked');
				if (this.isDisabled) classes.push('u-checkbox__icon-wrap--disabled');
				if (this.value && this.isDisabled) classes.push('u-checkbox__icon-wrap--disabled--checked');
				
				return classes.join(' ');
			},
			checkboxStyle() {
				let style = {};
				if(this.parent && this.parent.width) {
					style.width = this.parent.width;
					// #ifdef MP
					
					style.float = 'left';
					// #endif
					// #ifndef MP
					
					style.flex = `0 0 ${this.parent.width}`;
					// #endif
				}
				if(this.parent && this.parent.wrap) {
					style.width = '100%';
					// #ifndef MP
					
					style.flex = '0 0 100%';
					// #endif
				}
				return style;
			}
		},
		methods: {
			onClickLabel() {
				if (!this.isLabelDisabled && !this.isDisabled) {
					this.setValue();
				}
			},
			toggle() {
				if (!this.isDisabled) {
					this.setValue();
				}
			},
			emitEvent() {
				this.$emit('change', {
					value: !this.value,
					name: this.name
				})
				
				
				setTimeout(() => {
					if(this.parent && this.parent.emitEvent) this.parent.emitEvent();
				}, 80);
			},
			
			setValue() {
				
				let checkedNum = 0;
				if(this.parent && this.parent.children) {
					
					this.parent.children.map(val => {
						if (val.value) checkedNum++;
					})
				}
				
				if (this.value == true) {
					this.emitEvent();
					this.$emit('input', !this.value);
				} else {
					
					if(this.parent && checkedNum >= this.parent.max) {
						return this.$u.toast(`最多可选${this.parent.max}项`);
					}
					
					this.emitEvent();
					this.$emit('input', !this.value);
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	@import "../../libs/css/style.components.scss";

	.u-checkbox {
		/* #ifndef APP-NVUE */
		display: inline-flex;
		/* #endif */
		align-items: center;
		overflow: hidden;
		user-select: none;
		line-height: 1.8;
		
		&__icon-wrap {
			color: $u-content-color;
			flex: none;
			display: -webkit-flex;
			@include vue-flex;
			align-items: center;
			justify-content: center;
			box-sizing: border-box;
			width: 42rpx;
			height: 42rpx;
			color: transparent;
			text-align: center;
			transition-property: color, border-color, background-color;
			font-size: 20px;
			border: 1px solid #c8c9cc;
			transition-duration: 0.2s;
			
			/* #ifdef MP-TOUTIAO */
			
			&__icon {
				line-height: 0;
			}
			/* #endif */
			
			&--circle {
				border-radius: 100%;
			}
			
			&--square {
				border-radius: 6rpx;
			}
			
			&--checked {
				color: #fff;
				background-color: $u-type-primary;
				border-color: $u-type-primary;
			}
			
			&--disabled {
				background-color: #ebedf0;
				border-color: #c8c9cc;
			}
			
			&--disabled--checked {
				color: #c8c9cc !important;
			}
		}
	
		&__label {
			word-wrap: break-word;
			margin-left: 10rpx;
			margin-right: 24rpx;
			color: $u-content-color;
			font-size: 30rpx;
			
			&--disabled {
				color: #c8c9cc;
			}
		}
	}
</style>
